package com.security.Filter.MobileCaptchaFilter;

import com.alibaba.druid.util.StringUtils;
import com.security.Filter.SecurityFilter.SecurityFailureHandler;
import com.security.Utils.CaptchaUtils;
import com.security.system.entity.MobileCode;
import com.security.system.entity.SysUser;
import com.security.system.service.SysUserService;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;

@Component
public class MobileCapchaCodeFilter extends OncePerRequestFilter {

    @Resource
    SecurityFailureHandler failureHandler;

    @Resource
    SysUserService userService;

    @Override
    protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) throws ServletException, IOException {
        //判断 是否是 login 求情 并且 是 post
        if(StringUtils.equals("/mobilelogin",req.getRequestURI()) && StringUtils.equalsIgnoreCase(req.getMethod(),"post")){
            try {
                authMobileCapchaCode(new ServletWebRequest(req));
            }catch (AuthenticationException e){
                failureHandler.onAuthenticationFailure(req,res,e);
                return;
            }
        }
        //如果匹配继续执行 过滤链
        filterChain.doFilter(req,res);
    }

    private void authMobileCapchaCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
        HttpSession session = servletWebRequest.getRequest().getSession();
        //获取 请求 携带的 手机号 和 验证码
        String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(),"captcha");
        String mobileInRequest = servletWebRequest.getParameter("mobile");
        //获取 session 携带的 验证码
        MobileCode mobileCode = (MobileCode) session.getAttribute(CaptchaUtils.CAPTCHA_MOBILE_KEY);

        if(StringUtils.isEmpty(mobileInRequest)){
            throw new SessionAuthenticationException("手机号码不能为空");
        }

        if(StringUtils.isEmpty(codeInRequest)){
            throw new SessionAuthenticationException("短信验证码不能为空");
        }

        if(Objects.isNull(mobileCode)){
            throw new SessionAuthenticationException("短信验证码不存在");
        }

        if (mobileCode.isExpired()) {
            session.removeAttribute(CaptchaUtils.CAPTCHA_MOBILE_KEY);
            throw new SessionAuthenticationException("短信验证码已过期,请重新获取");
        }

        if (!mobileCode.getCode().equals(codeInRequest)) {
            throw new SessionAuthenticationException("短信验证码不正确");
        }

        if (!mobileCode.getMobile().equals(mobileInRequest)) {
            throw new SessionAuthenticationException("短信发送目标与您输入的不一致");
        }

        SysUser user = (SysUser)userService.loadUserByUsername(mobileInRequest);
        if (Objects.isNull(user)){
            throw new SessionAuthenticationException("您输入的的手机号未注册");
        }
        session.removeAttribute(CaptchaUtils.CAPTCHA_MOBILE_KEY);
    }


}
